#include "head.h"


//删除文件记录和文件的函数
bool delete_file(MYSQL *sql, int file_id, const char *file_path) {
    char query[4096];
    snprintf(query, sizeof(query), "SELECT id FROM files WHERE hash = (SELECT hash FROM files WHERE id = %d)", file_id);
    if (mysql_query(sql, query)) {
        fprintf(stderr, "%s\n", mysql_error(sql));
        return false;
    }

    MYSQL_RES *res = mysql_store_result(sql);
    if (mysql_num_rows(res) > 1) {
        //存在相同的hash，仅删除记录
        mysql_free_result(res);
    } else {
        //没有相同的hash，删除文件
        remove(file_path);
        mysql_free_result(res);
    }

    //删除记录
    snprintf(query, sizeof(query), "DELETE FROM files WHERE id = %d", file_id);
    if (mysql_query(sql, query)) {
        fprintf(stderr, "%s\n", mysql_error(sql));
    }
    return true;
}

bool delete_directory_and_contents(MYSQL *sql, const char *base_path, const char *user_id) {
    char query[4096];
    //查找所有在该目录下的文件夹和文件
    snprintf(query, sizeof(query), "SELECT id, path, file_type FROM files WHERE uid = '%s' AND path LIKE '%s%%'", user_id, base_path);
    if (mysql_query(sql, query)) {
        fprintf(stderr, "%s\n", mysql_error(sql));
        return false;
    }

    MYSQL_RES *res = mysql_store_result(sql);
    MYSQL_ROW row;
    //判断所有符合路径条件的是文件夹还是文件，文件夹仅删除记录即可
    while ((row = mysql_fetch_row(res))) {
        int file_id = atoi(row[0]);
        const char *file_path = row[1];
        int file_type = atoi(row[2]);

        //若是文件，则执行删除文件函数
        if (file_type == 0) {
            delete_file(sql, file_id, file_path);
        }
    }
    mysql_free_result(res);

    //删除所有记录
    snprintf(query, sizeof(query), "DELETE FROM files WHERE uid = '%s' AND path LIKE '%s%%'", user_id, base_path);
    if (mysql_query(sql, query)) {
        fprintf(stderr, "%s\n", mysql_error(sql));
        return false;
    }

    return true;
}

bool my_rm(sendMsg msg, client_status *user, int net_fd) {
    recvMsg rmsg;
    char query[8192];
    MYSQL_RES *res;
    MYSQL_ROW row;

    char base_path[4096];
    //printf("path=%s\n",user->path);
    //printf("path=%s\n",msg.msg);
    snprintf(base_path, sizeof(base_path), "%s%s", user->path, msg.msg);
    //printf("base_path=%s\n",base_path);
    /*
       if(!init_sql(&user.sql,user.user_name,user.password)){
       strcpy(rmsg.statu, "-1");
       strcpy(rmsg.msg, "数据库错误!");
       send(net_fd, &rmsg, sizeof(rmsg), 0);
       return false;
       }
       */
    //首先检查是文件还是文件夹
    snprintf(query, sizeof(query), "SELECT file_type FROM files WHERE uid = '%s' AND path = '%s'", user->user_id, base_path);
    if (mysql_query(user->sql, query)) {
        fprintf(stderr, "%s\n", mysql_error(user->sql));
        strcpy(rmsg.statu, "-1");
        strcpy(rmsg.msg, "数据库查询错误!");
        send(net_fd, &rmsg, sizeof(rmsg), 0);
        return false;
    }

    res = mysql_store_result(user->sql);
    if (mysql_num_rows(res) == 0) {
        strcpy(rmsg.statu, "-1");
        strcpy(rmsg.msg, "文件/文件夹不存在!");
        mysql_free_result(res);
        send(net_fd, &rmsg, sizeof(rmsg), 0);
        return false;
    }

    row = mysql_fetch_row(res);
    int file_type = atoi(row[0]);
    mysql_free_result(res);
    
    //若是文件夹
    if (file_type == 1) {
        if (!delete_directory_and_contents(user->sql, base_path, user->user_id)) {
            strcpy(rmsg.statu, "-1");
            strcpy(rmsg.msg, "删除文件夹失败!");
            send(net_fd, &rmsg, sizeof(rmsg), 0);
            return false;
        }
        //若是文件
    } else {
        snprintf(query, sizeof(query), "SELECT id, path FROM files WHERE uid = '%s' AND path = '%s'", user->user_id, base_path);
        if (mysql_query(user->sql, query)) {
            fprintf(stderr, "%s\n", mysql_error(user->sql));
            strcpy(rmsg.statu, "-1");
            strcpy(rmsg.msg, "数据库查询错误!");
            send(net_fd, &rmsg, sizeof(rmsg), 0);
            return false;
        }

        res = mysql_store_result(user->sql);
        row = mysql_fetch_row(res);
        int file_id = atoi(row[0]);
        const char *file_path = row[1];
        if(!delete_file(user->sql, file_id, file_path)){
            strcpy(rmsg.statu, "-1");
            strcpy(rmsg.msg, "删除文件失败!");
            send(net_fd, &rmsg, sizeof(rmsg), 0);
            return false;
        }
        mysql_free_result(res);
    }

    /*
    //判断文件是否存在
    snprintf(query, sizeof(query), "SELECT id, hash FROM files "
    "INNER JOIN files ON files.uid = user.uid"
    "WHERE uid = %s AND path = '%s'", user->user_id, msg.msg);
    if (mysql_query(user->sql, query)) {
    fprintf(stderr, "%s\n", mysql_error(user->sql));
    strcpy(rmsg.statu, "-1");
    strcpy(rmsg.msg, "数据库查询错误!");
    send(net_fd, &rmsg, sizeof(rmsg), 0);
    return false;
    }

    res = mysql_store_result(user->sql);
    //若文件不存在
    if (mysql_num_rows(res) == 0) {
    strcpy(rmsg.statu, "-1");
    strcpy(rmsg.msg, "文件不存在!");
    mysql_free_result(res);
    send(net_fd, &rmsg, sizeof(rmsg), 0);
    return false;
    }

    //检查是否存在相同的hash值
    //mysql_fetch_row取出的是字符串数组
    row = mysql_fetch_row(res);
    int file_id = atoi(row[0]);
    char *file_hash = row[1];

    snprintf(query, sizeof(query), "SELECT id FROM files WHERE hash = '%s'", file_hash);
    if (mysql_query(user->sql, query)) {
    fprintf(stderr, "%s\n", mysql_error(user->sql));
    } else {
    MYSQL_RES *hash_res = mysql_store_result(user->sql);
    if (mysql_num_rows(hash_res) > 1) {
    //存在相同的hash则不删除文件，仅删除记录
    hash_found = true;
    }
    mysql_free_result(hash_res);
    }

    //删除记录
    snprintf(query, sizeof(query), "DELETE FROM files WHERE id = %d", file_id);
    if (mysql_query(user->sql, query)) {
    fprintf(stderr, "%s\n", mysql_error(user->sql));
    }

    if (!hash_found) {
    //没有其他相同的hash，删除文件
    remove(msg.msg); 
    }
    */
    strcpy(rmsg.statu, "0");
    strcpy(rmsg.msg, "成功删除!");
    send(net_fd, &rmsg, sizeof(rmsg), 0);

    return true;
}
